﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Org.Eurekaa.DNJ.Helpers;
using System.Web.UI;
using Org.Eurekaa.DNJ.UI.DNJSettings;
using Org.Eurekaa.DNJ.Settings;

namespace Org.Eurekaa.DNJ.UI.WebControls
{
    
    public enum EnmDNJButtonType { button, checkbox, radio };

    [ToolboxBitmap(typeof(Org.Eurekaa.DNJ.UI.Resources.icons.DNJUIIcons), "DNJButton.bmp")]
    public class DNJButton : JQueryUIControl
    {
        private Button button = new Button();
        private CheckBox checkbox = new CheckBox();
        private RadioButton radio = new RadioButton();


        [DefaultValue(EnmDNJButtonType.button), Category("$.DNJ Settings")]
        public EnmDNJButtonType ButtonType
        {
            get;
            set;
        }

        [Category("$.DNJ Settings")]
        public String Text
        {
            get
            {
                switch (ButtonType)
                {
                    case EnmDNJButtonType.button:
                        {
                            return button.Text;                            
                        }
                    case EnmDNJButtonType.checkbox:
                        {
                            return checkbox.Text;
                        }
                    case EnmDNJButtonType.radio:
                        {
                            return radio.Text;
                        }
                }
                return button.Text;
            }
            set
            {
                button.Text = checkbox.Text = radio.Text = value;
            }
        }

        [Category("$.DNJ Settings")]
        [DefaultValue(false)]
        public bool Checked
        {
            get
            {
                switch (ButtonType)
                {
                    case EnmDNJButtonType.button:
                        {
                            return false;
                        }
                    case EnmDNJButtonType.checkbox:
                        {
                            return checkbox.Checked;
                        }
                    case EnmDNJButtonType.radio:
                        {
                            return radio.Checked;
                        }
                }
                return false;
            }
            set
            {
                checkbox.Checked = radio.Checked = value;
            }
        }


        [Category("$.DNJ Settings")]
        public String GroupName
        {
            get
            {
                switch (ButtonType)
                {
                    case EnmDNJButtonType.button:
                        {
                            return "";
                        }
                    case EnmDNJButtonType.checkbox:
                        {
                            return "";
                        }
                    case EnmDNJButtonType.radio:
                        {
                            return radio.GroupName;
                        }
                }
                return "";
            }
            set
            {
                radio.GroupName = value;
            }
        }


        private DNJButtonSettings _settings = new DNJButtonSettings();
        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("Settings")]
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DNJButtonSettings Settings
        {
            get { return _settings; }
            set { _settings = value; }
        }

        private DNJButtonEvents _events = new DNJButtonEvents();
        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("Events")]
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DNJButtonEvents ClientEvents
        {
            get { return _events; }
            set { _events = value; }
        }






        public override void SaveSettings(ref object stateObject)
        {
            object[] lstObj = new object[2];
            lstObj[0] = Settings;
            lstObj[1] = ClientEvents;

            stateObject = lstObj;

            GenericSettingsObject allSettings = new GenericSettingsObject();

            PageHelper.AppendJQueryScript(ClientEvents.GetDefaultEventsScript(), this.Page.Header);

            Settings.PrepareJSON();
            ClientEvents.PrepareJSON();
            allSettings.Append(Settings);
            allSettings.Append(ClientEvents);

            if (this.Parent != null && this.Parent.Parent != null && this.Parent.Parent is DNJButtonSet) return;

            String CtrlId = "";
            switch (ButtonType)
            {
                case EnmDNJButtonType.button:
                    {
                        CtrlId = button.ClientID;
                        break;
                    }
                case EnmDNJButtonType.checkbox:
                    {
                        CtrlId = checkbox.ClientID;
                        break;
                    }
                case EnmDNJButtonType.radio:
                    {
                        CtrlId = radio.ClientID;
                        break;
                    }
            }

            PageHelper.AppendJQueryPlugin("#" + CtrlId, "button", allSettings.ToString(), this.Page.Header);

        }


        public override void LoadSettings(object stateObject)
        {
            object[] lstObj = (object[])stateObject;
            Settings = (lstObj[0] as DNJButtonSettings);
            ClientEvents = (lstObj[1] as DNJButtonEvents);
        }



        protected override void Render(HtmlTextWriter writer)
        {
            if (this.DesignMode)
            {
                button.CssClass = "ui-button ui-widget ui-state-default ui-corner-all";
                button.Attributes.Add("style", "width:" + this.Width.ToString());
                button.RenderControl(writer);
            }
            else
                base.Render(writer);
        }

        protected override void CreateChildControls()
        {
            //Controls.Clear();
            switch (ButtonType)
            {
                case EnmDNJButtonType.button:
                    {
                        button.ID = "button" + this.ID;
                        button.Text = Text;
                        Controls.Add(button);
                        break;
                    }
                case EnmDNJButtonType.checkbox:
                    {
                        checkbox.ID = "checkbox" + this.ID;
                        checkbox.Text = Text;
                        Controls.Add(checkbox);
                        break;
                    }
                case EnmDNJButtonType.radio:
                    {
                        radio.ID = "radio" + this.ID;
                        radio.Text = Text;
                        Controls.Add(radio);
                        break;
                    }
            }


            base.CreateChildControls();

        }
    }

}

